import json
import random
import unittest

import requests

from lib.HTMLTestRunner_PY3 import HTMLTestRunner


def read_info(filename='data/login_hr.json'):
    temp_list = []
    with open(filename, 'r', encoding='utf-8') as f:
        result = json.load(f)
        # print(result)
        for temp_dict in result:
            temp_list.append(tuple(temp_dict.values()))
            # print(temp_list)
        return temp_list


def get_url(key, filename='data/url_hr.json'):
    with open(filename, 'r', encoding='utf-8') as k:
        result = json.load(k)
        # 字典取K_V的k值
        return result.get(key)


def deal_report(start_dir, pattern, filename):
    suite = unittest.TestLoader().discover(start_dir, pattern)
    with open(filename, 'wb') as f:
        runner = HTMLTestRunner(f, verbosity=2)
        runner.run(suite)


def get_au_header(json):
    login_url = get_url('url')
    resp = requests.post(login_url, json=json)
    # print(resp.json())
    # print(resp.json().get('data'))  查询api文档需求，提取au_header

    au_header = {'Authorization': 'Bearer ' + resp.json().get('data')}
    print(au_header)
    return au_header


def get_rand_mobile(start='18'):
    # 以start开头，后面跟一位4~9之间的任意一位数字，后面是8位随机数字
    temp = (start +
            str(random.randrange(4, 10)) +
            ''.join(str(random.choice(range(10))) for _ in range(8)))
    return temp


if __name__ == '__main__':
    ret = read_info()
    print(ret)

    ret = get_url('url')
    print(ret)
